package com.cskaoyan.service.system;

import com.cskaoyan.bean.BasePageInfo;
import com.cskaoyan.bean.po.ohter.MarketLog;
import com.cskaoyan.bean.po.ohter.MarketLogExample;
import com.cskaoyan.bean.vo.system.LogInfo;
import com.cskaoyan.bean.vo.system.LogListVo;
import com.cskaoyan.mapper.MarketLogMapper;
import com.cskaoyan.util.beanutils.BeanUtil;
import com.cskaoyan.util.common.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @auther zk
 * @date 2022/7/15 22:39
 */
@Service
public class LogServiceImpl implements LogService {

    @Autowired
    MarketLogMapper marketLogMapper;

    @Override
    public LogListVo logList(BasePageInfo info, String name) {
        PageHelper.startPage(info.getPage(), info.getLimit());

        MarketLogExample example = new MarketLogExample();
        example.setOrderByClause(info.getSort() + " " + info.getOrder());
        MarketLogExample.Criteria criteria = example.createCriteria();
        name = StringUtils.isEmpty(name) ? "%" : ("%" + name + "%");
        criteria.andAdminLike(name);
        criteria.andDeletedEqualTo(false);
        List<MarketLog> marketLogs = marketLogMapper.selectByExample(example);

        List<LogInfo> logInfos = new ArrayList<>();
        for (MarketLog marketLog : marketLogs) {
            LogInfo logInfo = new LogInfo();
            BeanUtil.copyBean(marketLog, logInfo);
            logInfos.add(logInfo);
        }
        PageInfo<MarketLog> pageInfo = new PageInfo<>(marketLogs);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();

        return new LogListVo((int) total, pages, info.getLimit(), info.getPage(), logInfos);
    }
}
